(function (window) {
    'use strict';
    var message = function () {
        this._init();
    };
    message.prototype = {
        _init: function () {
            if (typeof api === undefined) {
                console.log('该插件只能在apicloud下运行');
            } else {
                this.initPage();
            }
        },

        //step1:初始化页面
        initPage: function () {
            var self = this;

            $('#notice').dropload({
                scrollArea: window,
                domDown: {
                    domNoData: '<div class="dropload-noData">已经到底了</div>'
                },
                loadDownFn: function (me) {
                    $.ajax({
                        type: 'GET',
                        url: Config.common.getPushPageList,
                        data: {page: self.getCurrPage()},
                        dataType: 'json',
                        success: function (ret) {
                            if (ret.code === 1) {
                                //判断是否是最后一页
                                var res = ret.data;
                                if (res.length < 10 && res.length > 0) {
                                    self.createList(ret);
                                    me.lock();
                                    me.noData();
                                    me.resetload();
                                } else if (res.length === 0) {
                                    me.lock();
                                    me.noData();
                                    me.resetload();
                                } else {
                                    self.createList(ret);
                                    me.resetload();
                                }
                            }
                        },
                        error: function (xhr, type) {
                            // 即使加载出错，也得重置
                            me.resetload();
                        }
                    });
                }
            });
        }

        //step2:创建列表
        , createList: function (ret) {
            var self = this;

            var data = ret.data;
            var html = '';
            $.each(data, function (index, val) {
                html += '<li class="aui-list-item">';
                html += '<div class="aui-list-item-text">';
                html += '<div class="aui-list-item-title">'+val.content+'</div>';
                html += '<div class="aui-list-item-right">'+val.create_time+'</div>';
                html += '</div>';
                html += '</li>';
            });

            $("#notice_list").append(html)
        }

        //step3:获取当前页码
        ,getCurrPage: function () {
            var page = 0;
            var count = $('#notice_list li').length;
            if (count !== 0) {
                //计算当前页码
                page = Math.ceil(count / 10);
            }
            return page;
        }

    };
    window.message = message;
})(window);